/*-
 * Copyright (c) 1980, 1983, 1988, 1993
 *  The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *  This product includes software developed by the University of
 *  California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * -
 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies, and that
 * the name of Digital Equipment Corporation not be used in advertising or
 * publicity pertaining to distribution of the document or software without
 * specific, written prior permission.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 * -
 * --Copyright--
 */

/*
 *      @(#)netdb.h 8.1 (Berkeley) 6/2/93
 *      From: Id: netdb.h,v 8.9 1996/11/19 08:39:29 vixie Exp $
 * $FreeBSD: /repoman/r/ncvs/src/include/netdb.h,v 1.41 2006/04/15 16:20:26 ume Exp $
 */

/** @defgroup netdb Netdb
* @ingroup libc
*/

#ifndef _NETDB_H_
#define _NETDB_H_

#include <sys/cdefs.h>
#include <sys/types.h>
#include <sys/socket.h>

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */

#ifndef _PATH_HEQUIV
# define    _PATH_HEQUIV    "/system/etc/hosts.equiv"
#endif
#define _PATH_HOSTS "/system/etc/hosts"
#define _PATH_NETWORKS  "/system/etc/networks"
#define _PATH_PROTOCOLS "/system/etc/protocols"
#define _PATH_SERVICES  "/system/etc/services"

/*
 * Structures returned by network data base library.  All addresses are
 * supplied in host order, and returned in network order (suitable for
 * use in system calls).
 */
struct hostent {
    char    *h_name;    /* official name of host */
    char    **h_aliases;    /* alias list */
    int h_addrtype; /* host address type */
    int h_length;   /* length of address */
    char    **h_addr_list;  /* list of addresses from name server */
#define h_addr  h_addr_list[0]  /* address, for backward compatibility */
};

struct netent {
    char        *n_name;    /* official name of net */
    char        **n_aliases;    /* alias list */
    int     n_addrtype; /* net address type */
    uint32_t    n_net;      /* network # */
};

struct servent {
    char    *s_name;    /* official service name */
    char    **s_aliases;    /* alias list */
    int s_port;     /* port # */
    char    *s_proto;   /* protocol to use */
};

struct protoent {
    char    *p_name;    /* official protocol name */
    char    **p_aliases;    /* alias list */
    int p_proto;    /* protocol # */
};

struct addrinfo {
    int ai_flags;   /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
    int ai_family;  /* PF_xxx */
    int ai_socktype;    /* SOCK_xxx */
    int ai_protocol;    /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
    socklen_t ai_addrlen;   /* length of ai_addr */
    char    *ai_canonname;  /* canonical name for hostname */
    struct  sockaddr *ai_addr;  /* binary address */
    struct  addrinfo *ai_next;  /* next structure in linked list */
};

/*
 * Error return codes from gethostbyname() and gethostbyaddr()
 * (left in h_errno).
 */

#define NETDB_INTERNAL  -1  /* see errno */
#define NETDB_SUCCESS   0   /* no problem */
#define HOST_NOT_FOUND  1 /* Authoritative Answer Host not found */
#define TRY_AGAIN   2 /* Non-Authoritative Host not found, or SERVERFAIL */
#define NO_RECOVERY 3 /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */
#define NO_DATA     4 /* Valid name, no data record of requested type */
#define NO_ADDRESS  NO_DATA     /* no address, look for MX record */

/*
 * Error return codes from getaddrinfo()
 */
#define EAI_ADDRFAMILY   1  /* address family for hostname not supported */
#define EAI_AGAIN    2  /* temporary failure in name resolution */
#define EAI_BADFLAGS     3  /* invalid value for ai_flags */
#define EAI_FAIL     4  /* non-recoverable failure in name resolution */
#define EAI_FAMILY   5  /* ai_family not supported */
#define EAI_MEMORY   6  /* memory allocation failure */
#define EAI_NODATA   7  /* no address associated with hostname */
#define EAI_NONAME   8  /* hostname nor servname provided, or not known */
#define EAI_SERVICE  9  /* servname not supported for ai_socktype */
#define EAI_SOCKTYPE    10  /* ai_socktype not supported */
#define EAI_SYSTEM  11  /* system error returned in errno */
#define EAI_BADHINTS    12  /* invalid value for hints */
#define EAI_PROTOCOL    13  /* resolved protocol is unknown */
#define EAI_OVERFLOW    14  /* argument buffer overflow */
#define EAI_MAX     15

/*
 * Flag values for getaddrinfo()
 */
#define AI_PASSIVE  0x00000001 /* get address to use bind() */
#define AI_CANONNAME    0x00000002 /* fill ai_canonname */
#define AI_NUMERICHOST  0x00000004 /* prevent host name resolution */
#define AI_NUMERICSERV  0x00000008 /* prevent service name resolution */
/* valid flags for addrinfo (not a standard def, apps should not use it) */
#define AI_MASK \
    (AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST | AI_NUMERICSERV | \
    AI_ADDRCONFIG)

#define AI_ALL      0x00000100 /* IPv6 and IPv4-mapped (with AI_V4MAPPED) */
#define AI_V4MAPPED_CFG 0x00000200 /* accept IPv4-mapped if kernel supports */
#define AI_ADDRCONFIG   0x00000400 /* only if any address is assigned */
#define AI_V4MAPPED 0x00000800 /* accept IPv4-mapped IPv6 address */
/* special recommended flags for getipnodebyname */
#define AI_DEFAULT  (AI_V4MAPPED_CFG | AI_ADDRCONFIG)

/*
 * Constants for getnameinfo()
 */
#define NI_MAXHOST  1025
#define NI_MAXSERV  32

/*
 * Flag values for getnameinfo()
 */
#define NI_NOFQDN   0x00000001
#define NI_NUMERICHOST  0x00000002
#define NI_NAMEREQD 0x00000004
#define NI_NUMERICSERV  0x00000008
#define NI_DGRAM    0x00000010
/*
 * Scope delimit character
 */
#define SCOPE_DELIMITER '%'

#pragma GCC visibility push(default)

/* BIONIC-BEGIN */
#define  h_errno   (errno)
/* BIONIC-END */
void endhostent(void);
void endnetent(void);
void endnetgrent(void);
void endprotoent(void);
void endservent(void);
void freehostent(struct hostent *);
struct hostent  *gethostbyaddr(const void *, socklen_t, int);
int gethostbyaddr_r(const void *, socklen_t, int, struct hostent *, char *, size_t, struct hostent **, int *);

/**
* @ingroup  netdb
*
* @par Description:
* This API is used to resolve a hostname (string) into an IP_add.
*
* @param    name     [IN]  The hostname that is to be resolved.
*
* @attention
* <ul>
* <li>This API can give more than one IP_add.</li>
* <li>While parsing the multiple answer rcrds in DNS response message, if
* it encounters any malformed answer rcrd then it stops parsing and returns
* success if it has any successfully parsed rcrd or else it returns failure.</li>
* </ul>
*
* @retval  #hostent*  On success, pointer to the hostent structure is returned. On failure, NULL shall be returned.
*
* @par Dependency:
* <ul><li>netdb.h</li></ul>
*
* @see  None
*
* @since Huawei LiteOS V100R001C00
*/
struct hostent  *gethostbyname(const char *name);

/**
* @ingroup  netdb
*
* @par Description:
* This API is used to resolve a hostname (string) into an IP_add. This function is Thread-safe variant of gethostbyname().
*
* @param    name     [IN]   The hostname that is to be resolved.
* @param    ret      [OUT]  The pre-allocated struct where the result is stored.
* @param    buf      [IN]   The pre-allocated buffer where additional data is stored.
* @param    buflen   [IN]   The size of the buf.
* @param    result   [OUT]  This is a double pointer to hostent which is set to ret on success and set to zero on error scenario.
* @param    h_errnop [OUT]  It stores the errors.
*
* @attention
* <ul>
* <li>Additional error information is stored in *h_errnop instead of h_errno to be thread-safe.</li>
* <li>This API can give more than one IP_add also.</li>
* <li>While parsing the multiple answer rcrds in DNS response message, if
* it encounters any malformed answer rcrd then it stops parsing and returns
* success if it has any successfully parsed rcrd or else it returns failure.</li>
* </ul>
*
* @retval  #0     On success, pointer to the hostent structure is returned.
* @retval  #-1    On failure, the h_errnop variable will contain one among the following error codes in case of failure.
*                 HOST_NOT_FOUND : Hostname not found in DNS Server.
*                 EINVAL : Invalid arguments.
*                 ERANGE : Result not representable due to storage size limitations.
*
* @par Dependency:
* <ul><li>netdb.h</li></ul>
*
* @see  None
*
* @since Huawei LiteOS V100R001C00
*/
int gethostbyname_r(const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop);
struct hostent  *gethostbyname2(const char *, int);
int gethostbyname2_r(const char *, int, struct hostent *, char *, size_t, struct hostent **, int *);
struct hostent  *gethostent(void);
int gethostent_r(struct hostent *, char *, size_t, struct hostent **, int *);
struct hostent  *getipnodebyaddr(const void *, size_t, int, int *);
struct hostent  *getipnodebyname(const char *, int, int, int *);
struct netent   *getnetbyaddr(uint32_t, int);
int getnetbyaddr_r(uint32_t, int, struct netent *, char *, size_t, struct netent**, int *);
struct netent   *getnetbyname(const char *);
int getnetbyname_r(const char *, struct netent *, char *, size_t, struct netent **, int *);
struct netent   *getnetent(void);
int getnetent_r(struct netent *, char *, size_t, struct netent **, int *);
int getnetgrent(char **, char **, char **);
struct protoent *getprotobyname(const char *);
int getprotobyname_r(const char *, struct protoent *, char *, size_t, struct protoent **);
struct protoent *getprotobynumber(int);
int getprotobynumber_r(int, struct protoent *, char *, size_t, struct protoent **);
struct protoent *getprotoent(void);
int getprotoent_r(struct protoent *, char *, size_t, struct protoent **);
struct servent  *getservbyname(const char *, const char *);
struct servent  *getservbyport(int, const char *);
struct servent  *getservent(void);
void herror(const char *);
const char  *hstrerror(int);
int innetgr(const char *, const char *, const char *, const char *);
void sethostent(int);
void setnetent(int);
void setprotoent(int);

/**
* @ingroup  netdb
*
* @par Description:
*  This API is used to resolve a hostname (string) or a service name and into an IP_add.
*  And this addresses associated information to be used in creating a socket with which to address the
*  specified service.
*
* @param    nodename     [IN]   The descriptive name or address string of the host.
* @param    servname     [IN]   The servname points to string of port number or NULL.
* @param    hints        [IN]   The structure containing input values that set socktype and protocol.
* @param    res          [OUT]  The pointer to a pointer where to store the result (set to NULL on failure).
*
* @attention
* <ul>
* <li>Service names are not supported (only port numbers)!</li>
* </ul>
*
* @retval  #0            On success.
* @retval  #EAI_FAIL     The name server returned a permanent failure indication.
* @retval  #EAI_NONAME   The nodename and servname are NULL.
* @retval  #EAI_SERVICE  The requested servname is not available. Only port number is supported.
* @retval  #EAI_MEMORY   Out of memory.
*
* @par Dependency:
* <ul><li>netdb.h</li></ul>
*
* @see  None
*
* @since Huawei LiteOS V100R001C00
*/
int getaddrinfo(const char *nodename, const char *servname, const struct addrinfo *hints, struct addrinfo **res);

/**
* @ingroup  netdb
*
* @par Description:
* Converts a socket address to a corresponding host and service, in a
* protocol-independent manner. It is reentrant and allows programs to eliminate
* IPv4-versus-IPv6 dependencies.
* Memory for the result is allocated by the caller.
*
* @param    sa        [IN]   Indicates a pointer to a generic socket address structure (of type sockaddr_in or sockaddr_in6) that holds the input IP address and port number.
* @param    salen     [IN]   Indicates the size of the generic socket address structure "sa".
* @param    host      [OUT]  Indicates a pointer to caller-allocated buffer which will holds the null terminated hostname string.
* @param    hostlen   [IN]   Indicates the size of "host" buffer.
* @param    serv      [OUT]  Indicates a pointer to caller-allocated buffer which will holds the null terminated service-name string.
* @param    servlen   [IN]   Indicates the size of "serv" buffer.
* @param    flags     [IN]   Used to modify the behaviour of getnameinfo() and can have the following values:
*           NI_NAMEREQD : If set, then an error is returned if the hostname cannot be determined.
*           NI_DGRAM : If set, then the service is datagram (UDP) based rather than stream (TCP) based and causes getservbyport() 
*                      to be called with a second  argument of "udp" instead of its default of "tcp".  This is required for the few ports
*                      (512-514) that have different services for UDP and TCP.
*           NI_NOFQDN : If set, return only the hostname part of the fully qualified domain name for local hosts.
*           NI_NUMERICHOST : If set, then the numeric form of the hostname is returned. (When not set, this will still happen in case the node's name
*                            cannot be determined.)
*           NI_NUMERICSERV : If set, then the numeric form of the service address is returned.  (When not set, this will still happen in case the
*                            service's name cannot be determined.)
*
*
* @attention
* <ul>
* <li>This API is only supported since lwip 2.0.</li>
* <li>No support for translation of service names.</li>
* <li>Since there is no support for Service names, the flags - NI_DGRAM, NI_NUMERICSERV is not supported.</li>
* <li>NI_NOFQDN is not currently implemented.</li>
* <li>Reverse DNS Query for the IP Address, if needed, will be sent out immediately.</li>
* <li>If the time gap between the transmission of first Reverse DNS Query and the periodic
*     retransmission of the same query is less than 100ms, then the periodic retransmission
*     will be postponed by another 1100ms.</li>
* </ul>
*
* @retval #0             On success.
* @retval #EAI_FAIL      A nonrecoverable error occurred.
* @retval #EAI_FAMILY    The requested address family is not supported.
* @retval #EAI_NONAME    The name does not resolve for the supplied arguments. NI_NAMEREQD is set and the host's name cannot be located, or
*                        neither hostname nor service name were requested.
* @retval #EAI_OVERFLOW  The buffer pointed to by host or serv was too small.
* @retval #EAI_BADFLAGS  The "flags" argument has an invalid value.
*
* @par Dependency:
* <ul><li>netdb.h</li></ul>
*
* @see  None
*
* @since Huawei LiteOS V100R001C00
*/
int getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags);

/**
* @ingroup  netdb
*
* @par Description:
* This API is used to free one or more addrinfo structures. If the ai_next field
* of the structure is not null, the entire list of structures is freed.
*
* @param    ai     [IN]   The struct addrinfo to free.
*
* @attention
* <ul>
* <li>None.</li>
* </ul>
*
* @retval   #void     None.
*
* @par Errors
* <ul>
* <li>None</li>
* </ul>
*
* @par Dependency:
* <ul><li>netdb.h</li></ul>
*
* @see  None
*
* @since Huawei LiteOS V100R001C00
*/
void freeaddrinfo(struct addrinfo *ai);
const char  *gai_strerror(int);
void setnetgrent(const char *);
void setservent(int);

#pragma GCC visibility pop

#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */

#endif /* !_NETDB_H_ */
